package chai.ningning.process;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/sign/in")
public class SignInServlet extends HttpServlet{

	private static final long serialVersionUID = 1719889807579323044L;
	
	private final String filename = "user.properties";
	
	private String username ;
	private String password ;

	private void read() {

		Class<?> c = this.getClass();
		InputStream in = c.getResourceAsStream(filename);
		
		Properties prop = new Properties();
		
		try {
			prop.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		this.username = prop.getProperty("username");
		this.password = prop.getProperty("password");
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		
		this.read();
		
		String uname = req.getParameter("username");
		String pword = req.getParameter("password");
		
		resp.setContentType("text/html ; charset=UTF-8");
		PrintWriter out = resp.getWriter();
		
		if(username.equals(uname) && password.equals(pword)) {
			out.println("<div style='text-align:center ;font-wight:border ;font-size:20 ;'>");
			out.println("Welcome"+username);
			out.println("</div>");
		}else {
			out.println("<h1>用户名 或 密码错误</h1>");
			out.println("请重新登录");
		}
	}

}
